package com.leetcode.algorithm.y22.m08.w3;

import java.util.LinkedList;
import java.util.Queue;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 1302. 层数最深叶子节点的和
 * 
 * https://leetcode.cn/problems/deepest-leaves-sum/
 * 
 * @author jie.deng
 *
 */
class Question1302Solution01 {
	
	public int deepestLeavesSum(TreeNode root) {
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);
		int sum = 0;
		while (!queue.isEmpty()) {
			int len = queue.size();
			sum = 0;
			for (int i = 0; i < len; i++) {
				TreeNode node = queue.poll();
				sum += node.val;
				if (node.left != null) {
					queue.offer(node.left);
				}
				if (node.right != null) {
					queue.offer(node.right);
				}
			}
		}

		return sum;
	}
    
}